package com.yidian.controller;

import com.yidian.common.entity.Result;
import com.yidian.entity.Article;
import com.yidian.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(value = "/article", tags = "文章接口")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 根据频道id查询下面所有的文章(获取频道的文章列表)
     *
     // 根据文章id获取文章详情（获取文章详情）

     //根据文章id让收藏字段加一（收藏文章）

     // 根据文章id让收藏字段减一（取消收藏文章）

     // 根据文章id让点赞字段加一（点赞文章）

     // 根据文章id让点赞字段减一（取消点赞文章）
     */

    /**
     * 根据频道id查询下面所有的文章(获取频道的文章列表)
     *
     * @param channelId
     * @return
     */
    @RequestMapping("/all/{channelId}")
    @ApiOperation(value = "查询指定频道的文章", notes = "根据频道ID查询", httpMethod = "GET", response = Result.class)
    public Result<List<Article>> getAllArticle(@PathVariable("channelId") long channelId) {
        if (channelId == 0) {
            List<Article> articles = articleService.selectAllArticle();
            return Result.ok(articles);
        }
        List<Article> articles = articleService.selectAllArticleByChannelId(channelId);
        return Result.ok(articles);
    }

    /**
     * 根据文章id获取文章详情（获取文章详情）
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/one/{articleId}")
    @ApiOperation(value = "查看文章详情", notes = "根据文章ID查询", httpMethod = "GET", response = Result.class)
    public Result<Article> getArticle(@PathVariable("articleId") long articleId) {
        Article article = articleService.selectArticleById(articleId);
        return Result.ok(article);
    }

    /**
     * 根据文章id让收藏字段加一（收藏文章）
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/addCollect/{articleId}")
    @ApiOperation(value = "收藏文章", notes = "根据文章ID收藏", httpMethod = "GET", response = Result.class)
    public Result addArticleCollect(@PathVariable("articleId") long articleId) {
        articleService.updateArticleCollectAddCountByArticleId(articleId);
        return Result.ok();
    }

    /**
     * 根据文章id让收藏字段减一（取消收藏文章）
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/subtractCollect/{articleId}")
    @ApiOperation(value = "取消收藏文章", notes = "根据文章ID取消收藏", httpMethod = "GET", response = Result.class)
    public Result subtractArticleCollect(@PathVariable("articleId") long articleId) {
        articleService.updateArticleCollectSubtractCountByArticleId(articleId);
        return Result.ok();
    }

    /**
     * 根据文章id让点赞字段加一（点赞文章）
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/addLike/{articleId}")
    @ApiOperation(value = "点赞文章", notes = "根据文章ID点赞", httpMethod = "GET", response = Result.class)
    public Result addArticleLike(@PathVariable("articleId") long articleId) {
        articleService.updateArticleLikeAddCountByArticleId(articleId);
        return Result.ok();
    }

    /**
     * 根据文章id让点赞字段减一（取消点赞文章）
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/subtractLike/{articleId}")
    @ApiOperation(value = "取消点赞文章", notes = "根据文章ID点赞", httpMethod = "GET", response = Result.class)
    public Result subtractArticleLike(@PathVariable("articleId") long articleId) {
        articleService.updateArticleLikeSubtractCountByArticleId(articleId);
        return Result.ok();
    }

    @RequestMapping("/selectArticle")
    @ApiOperation(value = "查看所有文章", httpMethod = "GET", response = Result.class)
    public Result selectArticle() {
        List<Article> articles = articleService.selectArticle();
        return Result.ok(articles);
    }


}
